<?php

class userController extends baseController {
	
	public function index() {
	}
	
	public function registration() {
		
		if ($this->registry->router->getPostValue ( 'submit' )) {
			$user = new User ();
			
			$user_login = $this->registry->router->getPostValue ( 'login' );
			$user_password = $this->registry->router->getPostValue ( 'password' );
			$user_password_repeat = $this->registry->router->getPostValue ( 'password_repeat' );
			$user_email = $this->registry->router->getPostValue ( 'email' );
			
			$form_errors = array ();
			
			//if we have error, set error=true. Use for future checking
			$form_error = false;
			$form_errors ['login'] = '';
			$form_errors ['password'] = '';
			$form_errors ['email'] = '';
			
			if ($user->userExists ( $user_login )) {
				$form_errors ['login'] .= "<br>Login exists";
				$form_error = true;
			}
			
			if (! filter_var ( $user_login, FILTER_VALIDATE_REGEXP, array ("options" => array ("regexp" => "/[0-9A-Za-z]/" ) ) )) {
				$form_errors ['login'] .= "<br>Use only alphabet and digits";
				$form_error = true;
			}
			
			if (! filter_var ( $user_password, FILTER_VALIDATE_REGEXP, array ("options" => array ("regexp" => "/[0-9A-Za-z]/" ) ) )) {
				$form_errors ['password'] .= "<br>Use only alphabet and digits";
				$form_error = true;
			}
			
			if (($user_password !== $user_password_repeat) || $user_password === '') {
				$form_errors ['password'] .= "<br>Password are not similar";
				$form_error = true;
			}
			
			if (! filter_var ( $user_email, FILTER_VALIDATE_EMAIL )) {
				$form_errors ['email'] .= "<br>Email is incorrect";
				$form_error = true;
			}
			
			if (! $form_error) {
				$user->addUser ( $user_login, $user_password, $user_email );
				$form_errors ['success'] = "<br>Welcome;)";
			}
			$this->registry->template->form_errors = $form_errors;
		
		}
	
	}
	
	public function logout() {
		Auth::logout ();
		$this->registry->router->redirect ( 'index', 'index' );
	}
	
	
	public function view() {
		$user = new User();
		
		$user_id = $this->registry->router->getParam( 'id' );
		$user_info = $user->getUserInfo($user_id);
		$this->registry->template->user_info =  $user_info;
		
	}

}

?>

